package org.cpar.compiler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenRewriteStream;

final public class CPARCompiler {

	public static void compile(InputStream input, OutputStream output)
			throws IOException {
		SCPARLexer lex = new SCPARLexer(new ANTLRInputStream(input));
		TokenRewriteStream tokens = new TokenRewriteStream(lex);
		SCPARParser g = new SCPARParser(tokens);
		g.setTreeAdaptor(new CPARTreeAdaptor());
		CANSICodeWriter cw = new CANSICodeWriter(tokens, output);
		try {
			SCPARParser.translation_unit_return ret = g.translation_unit();
			CPARNode tree = (CPARNode) ret.getTree();

			//System.out.println(tree.toStringTree());

			tree.accept(cw);

			output.write(tokens.toString().getBytes());
			output.flush();
		} catch (RecognitionException e) {
			e.printStackTrace();
		}
	}
}
